package com.dao;

import com.model.Blog;
import com.utils.DbUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jinyb on 2017/5/4.
 */
public class BlogDao {
    //这个connection表示数据库的链接
    private Connection connection;

    public BlogDao() {
        //在这里初始化
        connection = DbUtil.getConnection();
    }


    /**
     * 获得所有博客数据
     * 这里是请求博客数据
     * @return
     */
    public List<Blog> getAllBlogs() {
        List<Blog> blogs = new ArrayList<Blog>();
        try {
            Statement statement = connection.createStatement();
            //这里是查询select * from blogs
            ResultSet rs = statement.executeQuery("select * from blogs");
            while (rs.next()) {
                Blog blog = new Blog();
                blog.setTitleName(rs.getString("title_name"));
                blog.setContent(rs.getString("content"));

                blogs.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return blogs;
    }


}
